package org.example.spring.ioc.processor;

import org.example.spring.ioc.bean.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
//拦截所有Bean的后置处理器
public class MyTestBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("后置处理器postProcessAfterInitialization：" + beanName);
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("后置处理器postProcessBeforeInitialization：" + beanName);

        if (bean instanceof User) {
            User user = (User) bean;
            user.setName("后置处理器修改了name");
            return user;
        }

        return bean;
    }

}
